package net.csdra.wenhua.nanan.qiandao.service;

import net.csdra.wenhua.nanan.qiandao.domain.User;
import net.csdra.wenhua.nanan.qiandao.domain.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.UUID;

/**
 * Created by xiaomi on 2017/2/20.
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserRepository userRepository;

    @Override
    public User createUser(CreateUser createUser) {
        Assert.notNull(createUser.weixinId);
        String id = UUID.randomUUID().toString();
        User user = new User();
        BeanUtils.copyProperties(createUser, user);
        user.setId(id);
        userRepository.save(user);
        log.trace("createUser: {}", user);
        return user;
    }

    @Override
    public User updateUser(UpdateUser updateUser) {
        Assert.notNull(updateUser.getUserId());
        User user = userRepository.findOne(updateUser.getUserId());
        Assert.notNull(user);
        BeanUtils.copyProperties(updateUser, user);
        userRepository.save(user);
        log.trace("updateUser: {}", user);
        return user;
    }

    @Override
    public User getUserById(String id) {
        Assert.notNull(id);
        User user = userRepository.findOne(id);
        return user;
    }
}
